using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Messaging;
using System.Threading;

using miciv.Job;
using micivCore.Data;

namespace micivCore.Job
{
    public class ApplyTechniqueThreadedJob : ApplyTechniqueJob
    {
        protected Thread m_thread;

        public ApplyTechniqueThreadedJob(IUnitTechnique _technique, IUnit _unit, ApplyTechniqueCompletedHandler _callback)
            : base(_technique, _unit, _callback)
        {
            this.m_thread = null;
        }

        protected void ThreadMain()
        {
            bool bResult = this.Technique.Apply(this.Unit);
            if (null != this.Callback)
            {
                this.Callback(bResult);
            }
            this.m_bIsCompleted = true;
        }

        #region IJob Membres

        override public void Run()
        {
            if ((false == this.m_bIsRunning) && (false == this.m_bIsCompleted))
            {
                this.m_thread = new Thread(new ThreadStart(this.ThreadMain));
                this.m_thread.Start();
                this.m_bIsRunning = true;
            }
        }

        #endregion
    }
}
